package com.qfedu.edu.controller;

import com.qfedu.edu.pojo.User;
import com.qfedu.edu.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaobobo
 * @title: UserController
 * @projectName CD-Java-FY-2401-Springboot-Parent
 * @description: TODO
 * @date 2024/9/10  14:51
 */
@Controller
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 查询所有的用户信息
     *
     * @return
     */
    @GetMapping("toListPage")
    public String list(Model model) {
        List<User> userList = userService.findUserAll();
        model.addAttribute("userList", userList);
        return "index";
    }

    /**
     * 跳转到添加数据的页面
     *
     * @return
     */
    @GetMapping("toUserAddPage")
    public String toUserAddPage() {
        return "user_add";
    }


    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("addUser")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/toListPage";
    }


    /**
     * 删除用户数据
     *
     * @param userId
     * @return
     */
    @GetMapping("deleteById")
    public String deleteById(Integer userId) {
        userService.deleteById(userId);
        return "redirect:/toListPage";
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/toListPage";
    }

    /**
     * 通过用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("findUserById")
    public String findUserById(Integer userId, Model model) {
        User user = userService.findUserById(userId);
        model.addAttribute("user", user);
        return "user_edit";
    }




}
